﻿using System;
using System.Windows.Forms;
using SmartDeviceProject1.Data;

namespace SmartDeviceProject1.Forms
{
    public partial class CategoryForm : Form
    {           
        private readonly DBHelper dbHelper = new DBHelper();
        private readonly CategoryType categoryType;
        public CategoryForm(CategoryType catType)
        {
            InitializeComponent();
            categoryType = catType;
            FormatTypeRelatedString(categoryType);
        }
        private void FormatTypeRelatedString(CategoryType catType)
        {
            //To do: Format The Type Related String For UI design
            // Considering the globalazation, going to postponed this requirement 
            Text += "(" + catType + ")";
        }

        private void addCategory_Click(object sender, EventArgs e)
        {
            string title = textBox1.Text;
            if (string.IsNullOrEmpty(title))
            {
                MessageBox.Show("Category Name Can't be Empty.");
                textBox1.Focus();
            }
            else
            {
                dbHelper.InsertCategory(title, categoryType, "");
                MessageBox.Show(string.Format("Category {0} has inserted.", title));
                textBox1.Text = string.Empty;
                payCategoryTableAdapter.Fill(mobileWalletDBDataSet.PayCategory);
            }
        }

        private void CategoryForm_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'mobileWalletDBDataSet.PayCategory' table. You can move, or remove it, as needed.
            payCategoryTableAdapter.AlertQuery(categoryType);
            payCategoryTableAdapter.Fill(mobileWalletDBDataSet.PayCategory);
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            Guid g = (Guid)cbDeleteList.SelectedValue;
            dbHelper.DeleteCategory(g);
            payCategoryTableAdapter.Fill(mobileWalletDBDataSet.PayCategory);
        }

        private void btChange_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbNewTitle.Text))
            {
                MessageBox.Show("New Category Name can't be Empty.");
                tbNewTitle.Focus();
            }
            else
            {
                Guid g = (Guid) cbChangeList.SelectedValue;
                dbHelper.ChangeCategoryTitle(g, tbNewTitle.Text, categoryType);
                tbNewTitle.Text = string.Empty;
                payCategoryTableAdapter.Fill(mobileWalletDBDataSet.PayCategory);
            }
        }
    }
    public enum CategoryType
    {
        Spend = 1,
        Earn = 0
    }
}